package cn.kooki.app.duobao.ui.Adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckedTextView;
import android.widget.TextView;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.kooki.app.duobao.R;
import cn.kooki.app.duobao.util.ScreenUtils;

/**
 * Created by xiong on 2016/1/8.
 */
public class PointsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private final ArrayList<Integer> potins;
    private boolean issing;
    public int currtday = 0;

    public PointsAdapter(ArrayList<Integer> potins) {
        this.potins = potins;
    }

    public void setCurrtday(int currtday) {
        this.currtday = currtday;
    }

    public void setIssing(boolean issing) {
        this.issing = issing;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_me_points_sign, parent, false);
        ViewGroup.LayoutParams layoutParams = inflate.getLayoutParams();
        layoutParams.width = ScreenUtils.getScreenWidth(parent.getContext()) / 5;
        inflate.setLayoutParams(layoutParams);
        return new PointsViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {


        if (holder instanceof PointsViewHolder) {
            PointsViewHolder viewHolder = (PointsViewHolder) holder;
            if (potins.size() > 0) {
                int value = potins.get(position);
                viewHolder.tvItemMePointsSignNumber.setText("+" + value);
                viewHolder.tvItemMePointsSignDay.setText("第" + (currtday + position - 1) + "天");
                viewHolder.tvItemMePointsSignPoint.setText(value + "积分");

                if (position < 2) {

                    viewHolder.rootview.setVisibility(value > 0 ? View.VISIBLE : View.INVISIBLE);
                    viewHolder.tvItemMePointsSignLine.setVisibility(currtday + position-1 > 1 ? View.VISIBLE : View.INVISIBLE);
                    viewHolder.tvItemMePointsSignLine2.setChecked(position < 1 || issing);

                    viewHolder.tvItemMePointsSignNumber.setSelected(true);
                    viewHolder.tvItemMePointsSignLine.setChecked(true);
                    viewHolder.tvItemMePointsSignStep.setChecked(true);
                    viewHolder.tvItemMePointsSignNumber.setVisibility(View.VISIBLE);

                } else if (position == 2) {
                    viewHolder.tvItemMePointsSignLine.setVisibility(currtday + position-1 > 1 ? View.VISIBLE : View.INVISIBLE);
                    viewHolder.rootview.setVisibility(View.VISIBLE);
                    viewHolder.tvItemMePointsSignLine.setChecked(issing);
                    viewHolder.tvItemMePointsSignNumber.setSelected(issing);
                    viewHolder.tvItemMePointsSignStep.setChecked(issing);
                    viewHolder.tvItemMePointsSignLine2.setChecked(false);
                    viewHolder.tvItemMePointsSignNumber.setVisibility(View.VISIBLE);
                } else {
                    viewHolder.tvItemMePointsSignLine.setVisibility(View.VISIBLE);
                    viewHolder.rootview.setVisibility(View.VISIBLE);
                    viewHolder.tvItemMePointsSignStep.setChecked(false);
                    viewHolder.tvItemMePointsSignNumber.setSelected(false);
                    viewHolder.tvItemMePointsSignLine2.setChecked(false);
                    viewHolder.tvItemMePointsSignNumber.setVisibility(View.INVISIBLE);
                }
            }
        }
    }


    @Override
    public int getItemCount() {
        return 5;
    }

    class PointsViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.tv_item_me_points_sign_number)
        TextView tvItemMePointsSignNumber;
        @Bind(R.id.tv_item_me_points_sign_line)
        CheckedTextView tvItemMePointsSignLine;
        @Bind(R.id.tv_item_me_points_sign_step)
        CheckedTextView tvItemMePointsSignStep;
        @Bind(R.id.tv_item_me_points_sign_line2)
        CheckedTextView tvItemMePointsSignLine2;
        @Bind(R.id.tv_item_me_points_sign_day)
        TextView tvItemMePointsSignDay;
        @Bind(R.id.tv_item_me_points_sign_point)
        TextView tvItemMePointsSignPoint;
        @Bind(R.id.rootView)
        View rootview;

        public PointsViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

    }
}
